/*
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
    (C)2013 Semtech

Description: Target board general functions implementation

License: Revised BSD License, see LICENSE.TXT file include in the project

Maintainer: Miguel Luis and Gregory Cristian
*/
#ifndef __BOARD_H__
#define __BOARD_H__

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "stm32f1xx.h"
#include "stm32f1xx_hal.h"
#include "utilities.h"
#include "timer.h"
#include "delay.h"
#include "gpio.h"
#include "spi.h"
//#include "uart.h"
#include "radio.h"
#include "sx1276/sx1276.h"
#include "rtc-board.h"
#include "sx1276-board.h"
//#include "uart-board.h"

#include "dht11.h" //llb add.  dht11
#include "dma.h"   //llb add. uart
#include "usart.h" //llb add. uart
#include "adc-eiri.h"
#include "i2c-eiri.h"

#if defined( USE_USB_CDC )
#include "uart-usb-board.h"
#endif

#define MAIN_VERSION   "V2.3.1"

/*!
 * Define indicating if an external IO expander is to be used
 */
//#define BOARD_IOE_EXT

/*!
 * Generic definition
 */
#ifndef SUCCESS
#define SUCCESS                                     1
#endif

#ifndef FAIL
#define FAIL                                        0
#endif

/*!
 * Board IO Extender pins definitions
 */

/*!
 * Board MCU pins definitions
 */
 
 
// LoRa Pin Define
#define RADIO_DIO_0                                 PC_4
#define RADIO_DIO_1                                 PC_5
#define RADIO_DIO_2                                 PA_7
#define RADIO_DIO_3                                 PA_6
#define RADIO_RESET                                 PB_1
#define RADIO_NSS                                   PB_12
#define RADIO_SCLK                                  PB_13
#define RADIO_MISO                                  PB_14
#define RADIO_MOSI                                  PB_15

//Ex OSC Pin Define
#define OSC_LSE_IN                                  PC_14
#define OSC_LSE_OUT                                 PC_15

//I2C2 Pin Define
#define I2C2_SCL                                    PB_10
#define I2C2_SDA                                    PB_11

//JLink Pin Define
#define SWDIO                                       PA_13
#define SWCLK                                       PA_14


//Ex. DHT11 Pin Define 
#define DHT11DQ_PIN                                 PA_0

//RS485 UART2 Pin Define for Debug
#define RS485_2_DIR_PIN                             PA_1
#define RS485_2_RXD_PIN                             PA_3
#define RS485_2_TXD_PIN                             PA_2

//RS485 UART1 Pin Define for ExConn.
#define RS485_1_DIR_PIN                             PA_8
#define RS485_1_RXD_PIN                             PA_10
#define RS485_1_TXD_PIN                             PA_9

//UART3 Pin Define for NB Conn.
#define UART3_RXD_PIN                               PC_11
#define UART3_TXD_PIN                               PC_10

//Ex. Hard Watching Dog Pin Define
#define EX_HWD_PIN                                  PB_6

//Ex. Encrypt Chip Pin Define
#define ENCRYPT_PIN                                 PB_8

// CH Window Pin Define
#define NET_LED_R                                   GPIOC
#define NET_LED_R_PIN                               GPIO_PIN_8
#define NET_LED_G                                   GPIOC
#define NET_LED_G_PIN                               GPIO_PIN_7

//DO_Pin HWRelay
#define DO_OUT                                      GPIOB
#define DO_OUT_PIN                                  GPIO_PIN_5

#define DIN_PORT                                    GPIOC
#define DIN_PIN                                     GPIO_PIN_2

/*!
 * Board MCU unusedpins definitions
 */
//#define UNUSEDPINPA0                                    PA_0
//#define UNUSEDPINPA1                                    PA_1
//#define UNUSEDPINPA2                                    PA_2
//#define UNUSEDPINPA3                                    PA_3
//#define UNUSEDPINPA4                                    PA_4
//#define UNUSEDPINPA5                                    PA_5
//#define UNUSEDPINPA6                                    PA_6
//#define UNUSEDPINPA7                                    PA_7
//#define UNUSEDPINPA8                                    PA_8
//#define UNUSEDPINPA9                                    PA_9
//#define UNUSEDPINPA10                                   PA_10
#define UNUSEDPINPA11                                   PA_11
#define UNUSEDPINPA12                                   PA_12
//#define UNUSEDPINPA13                                   PA_13
//#define UNUSEDPINPA14                                   PA_14
//#define UNUSEDPINPA15                                   PA_15 //JTDI

//#define UNUSEDPINPB0                                    PB_0
//#define UNUSEDPINPB1                                    PB_1
//#define UNUSEDPINPB2                                    PB_2  //boot1
//#define UNUSEDPINPB3                                    PB_3  //JTDO
//#define UNUSEDPINPB4                                    PB_4  //JTNRST
#define UNUSEDPINPB5                                    PB_5
#define UNUSEDPINPB6                                    PB_6
#define UNUSEDPINPB7                                    PB_7
//#define UNUSEDPINPB8                                    PB_8
#define UNUSEDPINPB9                                    PB_9
//#define UNUSEDPINPB10                                   PB_10
//#define UNUSEDPINPB11                                   PB_11
//#define UNUSEDPINPB12                                   PB_12
//#define UNUSEDPINPB13                                   PB_13
//#define UNUSEDPINPB14                                   PB_14
//#define UNUSEDPINPB15                                   PB_15

#define UNUSEDPINPC0                                    PC_0
#define UNUSEDPINPC1                                    PC_1
#define UNUSEDPINPC6                                    PC_6
#define UNUSEDPINPC7                                    PC_7
#define UNUSEDPINPC12                                   PC_12
#define UNUSEDPINPC13                                   PC_13

#define UNUSEDPINPD2                                    PD_2

//extern Gpio_t rftrx1;
//extern Gpio_t rftrx2;

typedef enum
{
    TX = (uint8_t)0,
    RX,
    NONE
}RFStates_typedef;


extern Gpio_t Ex_HWD;


#define DO_OUT_ON()  HAL_GPIO_WritePin(DO_OUT,DO_OUT_PIN,1)
#define DO_OUT_OFF() HAL_GPIO_WritePin(DO_OUT,DO_OUT_PIN,0)
#define DIN_Value()  HAL_GPIO_ReadPin(DIN_PORT, DIN_PIN)

#define LEDRX_ON()	HAL_GPIO_WritePin(NET_LED_G, NET_LED_G_PIN, 0)
#define LEDRX_OFF() HAL_GPIO_WritePin(NET_LED_G, NET_LED_G_PIN, 1)
#define LEDTX_ON()	HAL_GPIO_WritePin(NET_LED_R, NET_LED_R_PIN, 0)
#define LEDTX_OFF() HAL_GPIO_WritePin(NET_LED_R, NET_LED_R_PIN, 1)



/*!
 * MCU objects
 */
//extern Uart_t Uart1;
#if defined( USE_USB_CDC )
extern Uart_t UartUsb;
#endif



/*!
 * Possible power sources
 */
enum BoardPowerSources
{
    USB_POWER = 0,
    BATTERY_POWER,
};

/*!
 * \brief Disable interrupts
 *
 * \remark IRQ nesting is managed
 */
void BoardDisableIrq( void );

/*!
 * \brief Enable interrupts
 *
 * \remark IRQ nesting is managed
 */
void BoardEnableIrq( void );

/*!
 * \brief Initializes the target board peripherals.
 */
void BoardInitMcu( void );

/*!
 * \brief Initializes the boards peripherals.
 */
void BoardInitPeriph( void );

/*!
 * \brief De-initializes the target board peripherals to decrease power
 *        consumption.
 */
void BoardDeInitMcu( void );

/*!
 * \brief Measure the Battery voltage
 *
 * \retval value  battery voltage in volts
 */
uint32_t BoardGetBatteryVoltage( void );

/*!
 * \brief Get the current battery level
 *
 * \retval value  battery level [  0: USB,
 *                                 1: Min level,
 *                                 x: level
 *                               254: fully charged,
 *                               255: Error]
 */
uint8_t BoardGetBatteryLevel( void );

/*!
 * Returns a pseudo random seed generated using the MCU Unique ID
 *
 * \retval seed Generated pseudo random seed
 */
uint32_t BoardGetRandomSeed( void );

/*!
 * \brief Gets the board 64 bits unique ID
 *
 * \param [IN] id Pointer to an array that will contain the Unique ID
 */
void BoardGetUniqueId( uint8_t *id );

/*!
 * \brief Get the board power source
 *
 * \retval value  power source [0: USB_POWER, 1: BATTERY_POWER]
 */
uint8_t GetBoardPowerSource( void );

uint32_t HexToString(/*IN*/  const char    * pHex,  
                     /*IN*/  uint32_t           hexLen,  
                     /*OUT*/ char          * pByteString);

//IWDG add. ExHWD
void IWDG_Init(void);
void IWDG_FeedDog(void);

void RF_RxTx_LED_Init( void );
void RF_RxTx_LED( RFStates_typedef RxTx );

void Do_Din_Init( void );

void HAL_Delay(__IO uint32_t Delay);

#endif // __BOARD_H__
